// 游戏字幕
namespace ui {
	const bottomY = 500;
	const topY = 300;

	function moveLine(line: MsgLine, speed: number, callback: dramaCallback) {
		line.y = bottomY; // bottom.
		const time = ((bottomY - topY) / speed) * 1000;
		// line.horizontalCenter = 0.5;
		const tween = egret.Tween.get(line);
		tween
			.to({ scaleX: 0.5, scaleY: 0.5 }, 1)
			.to({ scaleX: 1.5, scaleY: 1.5 }, 300)
			.to({ y: topY, scaleX: 1, scaleY: 1, alpha: 0.5 }, time)
			.call(() => {
				safeRemoveFromFather(line);
				if (callback) {
					callback(1);
				}
			});
	}

	export class MsgScreen extends MsgLines {
		private static instance2: MsgScreen;

		public static override ins(): MsgScreen {
			if (!this.instance2) {
				this.instance2 = new MsgScreen();
			}
			return this.instance2;
		}

		async msgScreen(msg: string, speed: number, ms: number, callback: dramaCallback) {
			// const r = new eui.Rect(this.width, this.height, RGB(255, 0, 0));
			// r.alpha = 0.5;
			// this.addChild(r);
			this.visible = true;
			const lines = msg.split('#r');
			let index = 0;
			const len = lines.length;
			for (const l of lines) {
				const line = new MsgLine(l, 0, index);
				this.addChild(line);
				// line.fitTextWidth();
				if (index === len - 1) {
					moveLine(line, speed, null);
				} else {
					moveLine(line, speed, null);
					await waitMs(ms);
				}
				index++;
			}
			await waitMs(3000);
			callback(1);
			this.visible = false;
		}
	}
}
